package com.hui.auth.web;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hui.auth.dto.JsonResult;
import com.hui.auth.entity.AuthUser;
import com.hui.auth.exception.AuthError;
import com.hui.auth.service.AuthUserService;
import com.hui.operatelog.OperateType;
import com.hui.operatelog.OperationLog;
import com.hui.utils.ObjAnalysisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统用户管理
 * Created by LiaoYunHui on 2017/9/29/029.
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping(value = "auth/user")
public class UserController {
    @Autowired
    private AuthUserService userService;

    /**
     * 新增用户
     *
     * @param user
     * @param rileIds
     * @return
     */
    @ApiOperation(value = "新增用户")
    @PostMapping(value = "insert")
    @OperationLog(operation = OperateType.ADD, content = "用户")
    public JsonResult insert(@RequestBody Map<String, Object> requestBody) throws Exception {
        JsonResult jsonResult = new JsonResult();
        AuthUser user = (AuthUser) ObjAnalysisUtil.convertMap(AuthUser.class, (Map) requestBody.get("user"));
        List<Integer> roleIds = (List<Integer>) requestBody.get("roleIds");
        userService.insert(user, roleIds);
        return jsonResult;
    }

    /**
     * 查询所有用户
     *
     * @return
     */
    @OperationLog(operation = OperateType.SELECT, content = "用户列表")
    @ApiOperation(value = "查询所有用户")
    @GetMapping(value = "select_all")
    public JsonResult selectAll(@RequestParam Integer pageNo, @RequestParam Integer pageSize, @RequestParam String search) {
        JsonResult jsonResult = new JsonResult();
        Map<String, Object> data = new HashMap<>();
        Map<String, Object> params = new HashMap<>();
        params.put("search", search);
        Page p = PageHelper.startPage(pageNo, pageSize);
        List<AuthUser> userList = userService.selectAll(params);
        data.put("userList", userList);
        data.put("count", p.getTotal());
        jsonResult.setData(data);
        return jsonResult;
    }


    /**
     * 根据用户名查找用户信息
     *
     * @return
     */
    @ApiOperation(value = "根据用户名查找用户信息")
    @GetMapping(value = "select")
    @OperationLog(operation = OperateType.SELECT, content = "用户")
    public JsonResult select(@RequestParam String username) {
        JsonResult jsonResult = new JsonResult();
        Map<String, Object> data = new HashMap<>();
        Map map = userService.select(username);
        data.put("user", map.get("user"));
        data.put("roleIds", map.get("roleIds"));
        jsonResult.setData(data);
        return jsonResult;
    }

    /**
     * 通过用户ID修改用户
     *
     * @param user
     * @param roleIds
     * @return
     */
    @ApiOperation(value = "通过用户ID修改用户")
    @PostMapping(value = "update")
    public JsonResult updateUserById(@RequestBody Map<String, Object> requestBody) throws Exception {
        JsonResult jsonResult = new JsonResult();
        AuthUser user = (AuthUser) ObjAnalysisUtil.convertMap(AuthUser.class, (Map) requestBody.get("user"));
        List<Integer> roleIds = (List<Integer>) requestBody.get("roleIds");
        if (userService.update(user, roleIds)) {
            jsonResult.setData(true);
            return jsonResult;
        }
        jsonResult.setCode(AuthError.OPERATION_FAILED.code);
        jsonResult.setMsg(AuthError.OPERATION_FAILED.msg);
        return jsonResult;
    }

    /**
     * 删除用户
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除用户")
    @PostMapping(value = "delete/{id}")
    public JsonResult deleteUserById(@PathVariable Long id) {
        JsonResult jsonResult = new JsonResult();
        if (userService.delete(id)) {
            jsonResult.setData(true);
            return jsonResult;
        }
        jsonResult.setCode(AuthError.OPERATION_FAILED.code);
        jsonResult.setMsg(AuthError.OPERATION_FAILED.msg);
        return jsonResult;
    }
}
